package com.ybg.engine.mail;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.jdbc.core.JdbcTemplate;

import com.ybg.util.SysParamUtil;

public class StatiMailWrapper {
	String subject = "调度引擎工作状况邮件";
	MailInfo mMailInfo;
	Map<String,Object> contentMap  = new LinkedHashMap<String, Object>();
	public  void setSubject(String sub){
		subject = sub;
	}
	
	public  void addMsg(String key,Object value){
		contentMap.put(key, value);
	}
	public  void sendMail(JdbcTemplate jdbcTemplate){
		mMailInfo = MailInfo.getMailInfo(jdbcTemplate);//获取邮箱客户端信息
    	Object ob = SysParamUtil.getSimpleConfigValue(Constants.SWITCH_KEY);
    	if(ob == null || !ob.toString().equals(Constants.SWITCH_ON)){
    		return ;
    	}
		if(contentMap.size() == 0){
			//没有设置邮件内容的话禁止发送邮件
			return ;
		}
		
		String projectId = (String)SysParamUtil.getSimpleConfigValue(Constants.PROJECT_ID);
    	if(projectId == null){
    		projectId = "未知项目ID";
    	}
    	subject = projectId + " : " + subject;
		MailUtil.sendMail(jdbcTemplate,mMailInfo.getFrom(), mMailInfo.getTo(), subject, format(contentMap));
		contentMap.clear();
	}
	
	private  String format(Map<String,Object> contMap){
		StringBuffer contentBuffer = new StringBuffer();
		Iterator<Entry<String,Object>> iter = contMap.entrySet().iterator();
		while(iter.hasNext()){
			Entry<String,Object> entry = iter.next();
			contentBuffer.append(entry.getKey()+ " : " + entry.getValue()+ "\t\n");
		}
		return contentBuffer.toString();
	}
}
